#include "stdafx.h"

namespace xmlredirect
{
	Extracter::Context::Context(const string& path):m_xPath(path)
	{}
	void Extracter::Context::AddHandler( const OnMaterialFoundedType& slot )
	{
		m_onMaterialFounded.connect( slot );
	}

	Extracter::Extracter(const string& xmlName):m_xml(xmlName)
	{}
	Extracter::Extracter(xml::pdocument xmlDoc):m_xml(xmlDoc)
	{}

	void Extracter::Add(const SContext contextOfSearching)
	{
		m_Contexts.push_back( contextOfSearching );
	}
	void Extracter::Run()
	{
		list<SContext>::iterator it = m_Contexts.begin();
		list<SContext>::iterator it_end = m_Contexts.end();
		for (; it != it_end; ++it)
		{
			Parse( (*it), m_xml.get_root(), (*it)->m_xPath );
		}
	}
	void Extracter::Parse(SContext contextOfSearching, const xml::pnode& node, const string& xPath)
	{
		xml::pnodelist nodeList = node >> xml::nodelist( xPath );
		for(xml::pnode n = nodeList.nextnode(); n.valid(); n = nodeList.nextnode() ) 
		{ 
			try
			{
				contextOfSearching->m_onMaterialFounded( Extact(n) );
			}
			catch(const std::exception& exc)
			{
			}
		}
	}
	RawMaterial Extracter::Extact(xml::pnode& node)
	{
		node.get_name();

	}

	
}